package cc.rengu.igas.channel.wechat.core.service.trans;


import cc.rengu.igas.channel.wechat.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.wechat.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.wechat.core.service.base.PayService;
import cc.rengu.oltp.service.common.entity.DstChannelCfg;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.igas.channel.wechat.common.constant.*;
import cc.rengu.igas.channel.wechat.facade.request.WechatQuerySingleRefundRequest;
import cc.rengu.igas.channel.wechat.facade.response.WechatQuerySingleRefundResponse;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;


/**
 * 单笔退款状态查询
 */
public class WechatSingleRefundQueryService extends PayService {
    @Override
    protected Object beforeBizProcess() throws Exception {
        WechatQuerySingleRefundRequest querySingleRefundRequest = new WechatQuerySingleRefundRequest();
        ConvertUtil.convertOutput(querySingleRefundRequest);
        return querySingleRefundRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;

        WechatQuerySingleRefundResponse querySingleRefundResponse = new WechatQuerySingleRefundResponse();
        WechatQuerySingleRefundRequest querySingleRefundRequest = (WechatQuerySingleRefundRequest) request;
        DstChannelCfg dstChannelCfg = (DstChannelCfg) xmlTreeUtil.getXmlTreeObjectValue(WechatTreeNodeConstant.DST_CHANNEL_CFG, DstChannelCfg.class);
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(WechatTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);
        String instId = querySingleRefundRequest.getHeader().getInstId();
        ConvertUtil.convertInput(querySingleRefundRequest);
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, querySingleRefundRequest.getHeader().getTxnNum());
        xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.WX_CHANNEL_NO, dstChannelCfg.getChannelNo());
        xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.WX_MCHNT_NO, dstChannelCfg.getProviderMchntNo());
        xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.WX_APP_ID, dstChannelCfg.getAppId());
        if (ChannelEnum.NUWX.getChannelType().equals(querySingleRefundRequest.getChannelId())) {
            SysParamService sysParamService = new SysParamServiceImpl();
            SysParam sysParamUrl = sysParamService.getSysParamInfo(instId, WechatConfigConstant.NUCC_WECHAT_URL_CONF_NODE,
                    querySingleRefundRequest.getHeader().getTxnNum() + querySingleRefundRequest.getIdcFlag());
            if (sysParamUrl == null) {
                rglog.error("未配置网联交易<{}>地址idc标志<{}>，请配置", querySingleRefundRequest.getHeader().getTxnNum(), querySingleRefundRequest.getIdcFlag());
                throw new BizException(RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespCode(), RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespDesc());
            }
            xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.DEFINE_HTTP_URL_NODE, sysParamUrl.getParamValue());
        }
        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
        if (xmlTreeUtil.isExist(WechatTreeNodeConstant.DEFINE_HTTP_URL_NODE)) {
            xmlTreeUtil.deleteXmlTreeNode(WechatTreeNodeConstant.DEFINE_HTTP_URL_NODE);
        }
        if (0 != iReturnCode) {
            rglog.error("调用微信服务<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.TIME_OUT_ERROR.getRespCode(), RespCodeEnum.TIME_OUT_ERROR.getRespDesc());
        }
        ConvertUtil.convertOutput(querySingleRefundResponse);
        String resultCode = xmlTreeUtil.getXmlTreeStringValue(WechatTreeNodeConstant.WX_RESULT_CODE);
        String returnCode = xmlTreeUtil.getXmlTreeStringValue(WechatTreeNodeConstant.WX_RETURN_CODE);
        String returnMsg = xmlTreeUtil.getXmlTreeStringValue(WechatTreeNodeConstant.WX_RETURN_MSG);
        String errCode = xmlTreeUtil.getXmlTreeStringValue(WechatTreeNodeConstant.WX_ERR_CODE);
        String errMsg = xmlTreeUtil.getXmlTreeStringValue(WechatTreeNodeConstant.WX_ERR_MSG);
        if (!WechatParamConstant.STRING_SUCCESS.equals(resultCode)) {
            querySingleRefundResponse.setRespCode((null == errCode) ? returnCode : errCode);
            querySingleRefundResponse.setRespDesc((null == errMsg) ? returnMsg : errMsg);
            rglog.error("调用第三方服务失败,respCode=[{}],respDesc=[{}]", querySingleRefundResponse.getRespCode(), querySingleRefundResponse.getRespDesc());
        } else {
            querySingleRefundResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            querySingleRefundResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        }
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        chnlBizResponse.setResult(querySingleRefundResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        WechatQuerySingleRefundResponse querySingleRefundResponse = (WechatQuerySingleRefundResponse) response.getResult();
        ConvertUtil.convertInput(querySingleRefundResponse);
    }
}
